//
//  BorrowScenceModel.m
//  BrushFace
//
//  Created by timer_q on 15/12/24.
//  Copyright © 2015年 Zzz. All rights reserved.
//

#import "BorrowScenceModel.h"
#import "BorrowRequest.h"
#import "IWillHelper.h"
#import "NavStatus.h"

@implementation BorrowScenceModel
{
    
}
- (void)loadSceneModel{
    
    [super loadSceneModel];
    _planList = [NSMutableArray array];
}

- (void)hope_CreaterePayPlanCB:(BorrowPlan)cb{
    
    NSDictionary *params = @{
                             @"days" : [IWillHelper sharedInstance].days,
                             @"amount" : [IWillHelper sharedInstance].amount,
                             @"priod" : [IWillHelper sharedInstance].priod,
                             @"interestnum" : [IWillHelper sharedInstance].couponsid
                             };
    NSString *requestStr = [NSString stringWithFormat:@"%@/hope/createrepayplan.json",SERVICE_ADRESS];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        [_planList removeAllObjects];
        for (NSDictionary *subDict in [jsonObject objectForKey:@"data"]) {
            NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
            [newDict setObject:[subDict objectForKey:@"monthcapital"] forKey:@"monthcapital"];
            [newDict setObject:[subDict objectForKey:@"paymentdate"] forKey:@"paymentdate"];
            [newDict setObject:[subDict objectForKey:@"monthinterest"] forKey:@"monthinterest"];
            [_planList addObject:newDict];
        }
        cb(jsonObject);
        //ITLogSP(@"success:%@",jsonObject);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        ITLogSP(@"error:%@",error.description);
    }];
    
}

//发布借款
- (void)hope_Savehope:(void (^)(NSString *hopeID))CB{
    
    NSArray *reasonArr = @[@"我要租房",@"我要学习",@"我要旅游",@"我要买手机",@"我要生活费",@"生活周转"];
    NSString *reason;
    if ([IWillHelper sharedInstance].reasonID==6) {
        reason = [IWillHelper sharedInstance].reasonDetail;
    }else{
        reason = [reasonArr objectAtIndex:[IWillHelper sharedInstance].reasonID];
    }
    
    reason = [self replayRequestStr:reason];
    
    NSDictionary *params = @{
                             @"days" : [IWillHelper sharedInstance].days,
                             @"amount" : [IWillHelper sharedInstance].amount,
                             @"priod" : [IWillHelper sharedInstance].priod,
                             @"interestnum" : [IWillHelper sharedInstance].couponsid,
                             @"token" : [IWillHelper sharedInstance].token,
                             @"note": reason,
                             @"priodday": [NSString stringWithFormat:@"%d",[[IWillHelper sharedInstance].priod intValue]*30]
                             };
    NSString *requestStr = [NSString stringWithFormat:@"%@/hope/savehope.json",SERVICE_ADRESS];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        if ([[jsonObject objectForKey:@"result"] integerValue]==1) {
            CB([jsonObject objectForKey:@"relatedid"]);
        }else{
            NSString *errStr = errStr = [jsonObject objectForKey:@"errormsg"];;
            if ([errStr isEqual:[NSNull null]]||errStr==nil) {
                errStr = @"请求错误";
            }
            [[DialogUtil sharedInstance] showDlg:[[NavStatus sharedInstance] getCurWindow] textOnly:errStr];
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        ITLogSP(@"error:%@",error.description);
    }];
    
}
//发布借款前获取token
- (void)hope_getHopeCreation:(void (^)())CB{
    NSString *requestStr = [NSString stringWithFormat:@"%@/hope/gethopecreation.json",SERVICE_ADRESS];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    [manager GET:requestStr parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        [IWillHelper sharedInstance].token = [jsonObject objectForKey:@"token"];
        CB();
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        ITLogSP(@"error:%@",error.description);
    }];
    
}

- (void)common_fullnamed:(void (^)(BOOL isFull))CB{
    NSString *requestStr = [NSString stringWithFormat:@"%@/user/fullnamed.json" ,SERVICE_ADRESS];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    [manager GET:requestStr parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        if ([[jsonObject objectForKey:@"rtype"] integerValue]==0) {
            CB(YES);
        }else{
            CB(NO);
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        ITLogSP(@"error:%@",error.description);
    }];
}

- (void)user_isFace:(void (^)(BOOL isFace))CB{
    NSString *requestStr = [NSString stringWithFormat:@"%@/user/isface.json",SERVICE_ADRESS];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    [manager GET:requestStr parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        CB([operation.responseString boolValue]);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        ITLogSP(@"error:%@",error.description);
    }];

}

@end
